﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Zgy_vote.Models.Database;
using Zgy_vote.ser;

namespace Zgy_vote.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly Zgy_voteContext _db;
        private readonly IJWTService _jwtService;
        public HomeController(Zgy_voteContext db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }

        [HttpPost]
        public string Reg(string email, string password)
        {

            if (_db.Users.Any(x => x.Email == email))
            {
                return "邮箱已存在";
            }
            var user = new User()
            {
                Email = email,
                Password = password,
            };
            _db.Users.Add(user);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "注册成功";
            }
            return "注册失败";
        }

        [HttpPost]
        public string Login(string email, string password)
        {

            var user = _db.Users.FirstOrDefault(x => x.Email == email);

            if (user == null)
            {
                return "用户不存在";
            }
            if (user.Password != password)
            {
                return "密码不正确";
            }

            return _jwtService.CreateToken(user.Id);
        }
        /// <summary>
        /// 资料上传接口
        /// </summary>
        /// <param name="CompanyName"></param>
        /// <param name="Logo"></param>
        /// <param name="Jianjie"></param>
        /// <param name="Pcate"></param>
        /// <param name="Acate"></param>
        /// <param name="Pphoto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string AddList( string CompanyName , string Logo, string Jianjie, string Pcate, string Acate, string Pphoto)
        {
            //获取返回的tekon
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //上传列表
            var List = new Company()
            {
                UserId = Convert.ToInt32(Id),
                CompanyName = CompanyName,
                Logo = Logo,
                Jianjie = Jianjie,
                Pcate = Pcate,
                Acate = Acate,
                Pphoto = Pphoto,
                Ptime = DateTime.Now
            };
            _db.Companies.Add(List);
            var row = _db.SaveChanges();
            if (row > 0)
            {

                return "上传成功";
            }
            return "上传失败";
        }
        /// <summary>
        /// 校验用户是否上传过该类别的投票项目
        /// </summary>
        /// <param name="Pcate"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string Pcate(string Pcate)
        {
            //获取返回的tekon
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //通过查询数据库判断是否上传了
            var List = _db.Companies.FirstOrDefault(x => x.UserId == Convert.ToInt32(Id) && x.Pcate == Pcate);
            if (List == null)
            {
                return "否";
            }
            return "是";

        }
    }
}
